<template>
  <el-card shadow="never">
    <div slot="header">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品分类</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 主题区域 -->
    </div>
    <div class="filter-bar">
      <!-- 搜素区域 -->
      <div class="l-form">
        <el-input
          placeholder="请输入内容"
          size="small"
          v-model="query"
          clearable
          @clear="resetQuery"
        >
          <template slot="append"><i class="el-icon-search"  @click="serchQuery"></i></template>
        </el-input>
      </div>
      <!-- 添加区域 -->
      <div class="r-btn">
        <el-button type="primary" size="small" @click="$router.push('goods/add')">增加</el-button>
      </div>
    </div>
    <!-- 表格区域 -->
    <div class="table-box">
      <!-- 表格 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="索引" width="50px" align="center" ></el-table-column>
        <el-table-column prop="goods_id" label="Id" align="center" width="55px"> </el-table-column>
        <el-table-column label="商品名称" width="400px" prop="goods_name" align="center"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" align="center" width="110px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" align="center"></el-table-column>
        <el-table-column label="是否热销" align="center" >
          <template slot-scope="scope">
            <i class="el-icon-success" v-if="scope.row.is_promote" style="color:green;font-size:20px" ></i>
            <i class="el-icon-error" v-if="!scope.row.is_promote"  style="color:#4e6ef2;font-size:18px"></i>
          </template>
        </el-table-column>
        <el-table-column label="商品状态" prop="goods_state" align="center">
         <template slot-scope="scope">
            <el-tag v-if="scope.row.goods_state == 0" type="info" size="small"  effect="dark" >未通过</el-tag>
            <el-tag v-if="scope.row.goods_state == 1" type="warning" size="small"  effect="dark" >审核中</el-tag>
            <el-tag v-if="scope.row.goods_state == 2" type="success" size="small"  effect="dark" >已审核  </el-tag>
         </template>
        </el-table-column>
        <el-table-column label="添加时间" width="160px" align="center"  >
           <template slot-scope="scope">
                {{scope.row.add_time | formateTime}}
            </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
           <template slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              icon="el-icon-edit"
              @click="scope"
              circle
            ></el-button>
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              circle
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
       <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-size="pagesize"
        :page-sizes="[ 5, 10,15,20,25,30,35,40]"
        @size-change="sizeChange"
        @current-change="pageChange"
      >
      </el-pagination>
    </div>
    <!-- 增加表单弹框 -->
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      query: "",
      goodsList: [],
      pagenum: 1, // 默认页码
      pagesize: 10, // 获取页数
      userList: [], //列表数据
      total: 0, // 总条数
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    // 获取商品列表数据
    async getGoodsList() {
      let { data: res } = await this.$axios.get("goods", {
        params: {
          query: this.query,
          pagenum: this.pagenum,
          pagesize: this.pagesize,
        },
      });
      console.log(res);
      if (res.meta.status != 200) return this.$message.error(res.meta.msg);
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    // 页面数量更换事件
    sizeChange(val) {
      this.pagesize = val;
      this.getGoodsList();
    },
    // 页码更换书剑
    pageChange(val) {
      this.pagenum = val;
      this.getGoodsList();
    },
    // 关键字查询
    serchQuery() {
      this.pagenum = 1;
      this.getGoodsList();
    },
    // 重置关键字
    resetQuery() {
      this.pagenum = 1;
      this.getGoodsList();
    },
  },
};
</script>

<style lang="less" scoped>
</style>